# -*- coding: utf-8 -*-
"""
Created on Fri Dec 04 10:30:07 2015

@author: liliangrong
"""
#抓取页面源代码
def get_page_code(words,url=None):
    #import urllib
    import urllib2
    #import re
    user_agent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.154 Safari/537.36 LBBROWSER"
    headers = {"User-Agent":user_agent}
    if url == None:
        url = "http://www.baidu.com/baidu?wd=%s&tn=monline_4_dg" % words
    else:
        url = url
    try:
        req = urllib2.Request(url,None,headers)
        resp = urllib2.urlopen(req)
        page_source = resp.read()
        return page_source

    except urllib2.URLError,e:
        if hasattr(e,'code'):
            print e.code
        if hasattr(e,'reason'):
            print e.reason

def get_sort_url(words):
    import re
    page_source = get_page_code(words)
    #抓取排名序号和对应的网址前缀
    pattern = re.compile('<div.*?id="([\d]+)" .*?c-container.*?>.*?<span class="c-showurl">(.*?)</span>.*?</div>',re.S)
    #返回正则表达式处理后的内容
    sort_url = re.findall(pattern,page_source)
    return sort_url

def get_next_page(words):
    import re
    page_source = get_page_code(words)
    pattern = re.compile('<div id="page">.*?<a class="n" href="(.*?)">[\D]+</a></div>',re.S)
    page_num = re.findall(pattern,page_source)
    return page_num
    
def get_baidu_rank(site_url,words):
    loop = 0
    
    sort_urls = get_sort_url(words)
    while loop == 0:
        for sort,line in sort_urls:
            if line.find(site_url) >= 0:
                return site_url," : ",words,"|",sort
                #loop = 1
                break
            else:
                loop = 1
                #print "no match success!"
    else:
        print u"继续在其他页面查找数据!"
        page_nums = get_next_page(words)
        print page_nums
        #del page_nums[0]
        #print page_nums
        #for page_url in page_nums:
            
            #p_url = "http://www.baidu.com"+page_url
            #print p_url
            #print page_url
            #sort_urls = get_page_code(words,p_url)
            #print sort_urls
            #get_baidu_rank(site_url,words)
    
if __name__ == "__main__":
    import time
    start = time.clock()
    #word = raw_input("input word : ")
    #word = "PC/ABS"
    word = "abs"
    site_url = "www.kumhosunny.com"
    #site_url = raw_input("input site_url : ")
#    f = open('baidu_rank.txt','w')
    baidu_rank = get_baidu_rank(site_url,word)
    print baidu_rank
#    f.close()
        
    end = time.clock()
    print "run time is %f s" % (end-start)